package com.mmcg.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 作者：李嘉浩
 * 时间：2024-09-18-19:07
 * 功能：
 */

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();

        // 2. 判断session中是否有值
        Object name = session.getAttribute("name");

        // 3. 如果没有登录就跳转到登录页面
        if(name == null){
            response.sendRedirect("http://family.mmcg.com/login.html?returnUrl=http://cart.mmcg.com");
            return false;
        }

        return true;
    }
}
